﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.TextMining;

namespace SharedGenomics.Workbench.Tests.TextMining
{
    [TestClass]
    public class StringTilingTests
    {
        //score function sums the scores of all terms
        private ScoreTiling scoreFunc = new ScoreTiling(ts => ts.Where(t => t.IsTerm).Sum(t => t.CValue));
        private StringTiling tiling;

        [TestInitialize]
        public void Setup()
        {
            this.tiling = new StringTiling(this.scoreFunc);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ConstructorTestInvalidNull()
        {
            StringTiling st = new StringTiling(null);
        }

        [TestMethod]
        public void ConstructorTestValuesValidNull()
        {
            StringTiling st = new StringTiling(this.scoreFunc, null);
            Assert.AreEqual(0, st.Count());
        }

        [TestMethod]
        public void ConstructorTestValuesValid()
        {
            TileSection ts1 = new TileSection("text");
            TileSection ts2 = new TileSection(new ScoredTerm("term1", 0.1), 3);

            var tiles = new[] { ts1, ts2 };
            StringTiling tiling = new StringTiling(this.scoreFunc, tiles);

            CollectionAssert.AreEqual(tiles, tiling.ToArray());
        }

        [TestMethod]
        public void AddTileValid()
        {
            TileSection ts = new TileSection("text");
            this.tiling.Add(ts);

            CollectionAssert.AreEqual(new[] { ts }, this.tiling.ToArray());
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void AddTileInvalidNull()
        {
            this.tiling.Add(null);
        }

        [TestMethod]
        public void CalculateScoreTest()
        {
            //add some tile sections to the collection...
            this.tiling.Add(new TileSection("text"));                           //score: 0
            this.tiling.Add(new TileSection(new ScoredTerm("term1", 0.1), 1));  //score: 0.1
            this.tiling.Add(new TileSection(new ScoredTerm("term1", 0.4), 4));  //score: 0.4
            this.tiling.Add(new TileSection("text"));                           //score: 0
            this.tiling.Add(new TileSection(new ScoredTerm("term1", 0.3), 3));  //score: 0.3

            Assert.AreEqual(0.8, this.tiling.CalculateScore());
        }
    }
}
